﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using DevExpress.XtraGrid.Views.Grid;

namespace QL_NOI_TRU
{
    public partial class frmNhapDSPhongTruc : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapDSPhongTruc()
        {
            InitializeComponent();
        }

        private void frmNhapDSPhongTruc_Load(object sender, EventArgs e)
        {
            //đổ dữ liệu vào grid
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery("select MAPH as 'Mã phòng trực',KHOA.TEN_KHOA as 'Tên khoa', TEN_PHONG as 'Tên phòng trực' from KHOA, PHONG_TRUC where KHOA.MA_KHOA = PHONG_TRUC.MA_KHOA ");

            //đỗ dữ liệu vào cbbkhoa
            var a = "select * from KHOA";
            SqlConnection conn = new SqlConnection(frmDangnhap.stringConn);
            SqlCommand cm = conn.CreateCommand();
            cm.CommandText = a;
            conn.Open();
            SqlDataReader dr = cm.ExecuteReader();
            while (dr.Read())
            {
                cbbKhoa.Properties.Items.Add(String.Format("{0} | {1}", dr["MA_KHOA"], dr["TEN_KHOA"]));
            }
            dr.Close();
          //  txtTenPhong.DataBindings.Add("Ten_Khoa",gridControl1.DataSource,"Text");

        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            string strr = "EXEC Insert_PHONG_TRUC @MAPH = '" + txtMaPH.Text.ToString().Trim() + "',@TEN_PHONG = N'" + txtTenPhong.Text.ToString().Trim() + "',@MA_KHOA = '" + cbbKhoa.Text.Substring(0, 2).ToString().Trim() + "'";
            var kn = new Ketnoi(frmDangnhap.stringConn);
            kn.ExcuteQuery(strr);
            MessageBox.Show("Đã lưu thành công");
        }

        private void btXoaGiuong_Click(object sender, EventArgs e)
        {
            this.Hide();
            var f = new frmNhapDanhSachKhoa();
            f.Show();
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            txtTenPhong.Text = "";
            txtMaPH.Text = "";
            cbbKhoa.SelectedIndex = -1;
            frmNhapDSPhongTruc_Load(null, null);
        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;

            //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
            if (view.IsNewItemRow(e.RowHandle))
            {
                //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại
              //  string insert = "insert into hocsinh(hoten, ngaysinh, gioitinh,diachi) values ('"
              //      + view.GetRowCellValue(e.RowHandle, view.Columns[1]).ToString() + "','"
               //     + view.GetRowCellValue(e.RowHandle, view.Columns[2]).ToString() + "','"
               //     + view.GetRowCellValue(e.RowHandle, view.Columns[3]).ToString() + "','"
               //     + view.GetRowCellValue(e.RowHandle, view.Columns[4]).ToString() + "')";
                string tenkhoa = "select TEN_KHOA from KHOA"
               string insert = "exec EXEC Insert_PHONG_TRUC @MAPH = '" + view.GetRowCellValue(e.RowHandle,view.Columns[1]).ToString() + "',@TEN_PHONG = N'" + view.GetRowCellValue(e.RowHandle, view.Columns[3]).ToString() + "',@MA_KHOA = '" + view.GetRowCellValue(e.RowHandle, view.Columns[2]).ToString() + "'";
                
                var kn = new Ketnoi(frmDangnhap.stringConn);
                kn.ExcuteNonQuery(insert);

            }
            //Cũ thì update
            else
            {
                string t = "select MA_KHOA from KHOA,PHONG_TRUC where PHONG_TRUC.MA_KHOA = KHOA.MA_KHOA and KHOA.TEN_KHOA='" + view.GetRowCellValue(e.RowHandle, view.Columns[2]).ToString() + "')";
                string update = "update PHONG_TRUC set MAPH='"
                   + view.GetRowCellValue(e.RowHandle, view.Columns[1]).ToString() + "', MA_KHOA ='"
                   + t + "', TEN_PHONG ='"
                   + view.GetRowCellValue(e.RowHandle, view.Columns[3]).ToString();
                var kn = new Ketnoi(frmDangnhap.stringConn);
                kn.ExcuteNonQuery(update);

            }
            frmNhapDSPhongTruc_Load(null, null);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Delete row?", "Confirmation", MessageBoxButtons.YesNo) ==
                 DialogResult.Yes)
            {


                string id = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns[0]).ToString();
                string delete = "delete from hocsinh where id =" + id;
                con.Open();
                OdbcCommand ocmd = new OdbcCommand(delete, con);
                ocmd.ExecuteNonQuery();
                con.Close();
                LoadData();
            }
        }

    }
}